package com.rr.middle;

/**
 * @author RR
 * @date 2022/8/15 21:13
 */

/*
 * https://leetcode.cn/problems/search-a-2d-matrix/
 * */
public class SearchMatrixTow {
    public boolean searchMatrix(int[][] matrix, int target) {
        int length = matrix[0].length;
        for (int[] ints : matrix) {
            if (ints[0] <= target && ints[length - 1] >= target) {
                int left = 0;
                int right = length - 1;
                while (right >= left) {
                    int middle = (left + right) / 2;
                    if (ints[middle] == target) {
                        return true;
                    } else if (ints[middle] > target) {
                        right = middle - 1;
                    } else {
                        left = middle + 1;
                    }
                }
            }
        }
        return false;
    }

    public static void main(String[] args) {
        SearchMatrixTow searchMatrixTow = new SearchMatrixTow();
        System.out.println(searchMatrixTow.searchMatrix(new int[][]{{1}}, 1));
    }
}
